import java.util.Arrays;

public class Leetcode88 {
    public static void main(String[] args) {
        int[] k = new int[2];
        k[0] = 2;
        int[] l = new int[1];
        l[0] = 1;
        merge(k, 1, l, l.length);
        System.out.println(Arrays.toString(k));
    }

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        if (m == 0) {
            System.arraycopy(nums2, 0, nums1, 0, n);
            return;
        }

        int lastIndex = nums1.length - 1;
        while (lastIndex >= 0) {
            if (m - 1 < 0) {
                nums1[lastIndex] = nums2[n - 1];
                n--;
            } else if (n - 1 < 0) {
                nums1[lastIndex] = nums1[m - 1];
                m--;
            } else {
                if (nums1[m - 1] > nums2[n - 1]) {
                    nums1[lastIndex] = nums1[m - 1];
                    m--;
                } else {
                    nums1[lastIndex] = nums2[n - 1];
                    n--;
                }
            }

            lastIndex--;
        }
    }
}
